﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using KC.Framework.Extension;
using KC.Common.ToolsHelper;
using KC.Framework.Util;
using KC.Service.Core.Base;
using KC.Service.Core.Constants;
using ProtoBuf;

namespace KC.Service.Core.Message
{
    /// <summary>
    /// 华为短信平台
    /// </summary>
    public class SmsHuawei : AbstractSms
    {
        private const string ServiceName = "KC.Service.Core.Message.SmsHuawei";
        public SmsHuawei(SmsConfig config)
            : base(config)
        {
        }
        
        public override string Send(List<long> mobiles, string msgContent)
        {
            throw new NotImplementedException();
        }

        public override string SendVoice(List<long> mobiles, string msgContent)
        {
            var timestamp = DateTime.UtcNow.ToLocalDateTimeStr("yyyyMMddhhmmss");
            var requestData = new Request();
            var requestHead = new RequestHead();
            var requestBody = new RequestBody();
            requestData.Head = requestHead;
            requestData.Body = requestBody;
            requestHead.MethodName = Config.MethodName;
            requestHead.Spid = Config.Spid;
            requestHead.Appid = Config.Appid;
            requestHead.Passwd = Sha1Encrypt(Config.Passwd);
            requestHead.Timestamp = timestamp;
            requestHead.Authenticator = Sha1Encrypt(timestamp + Config.MethodName + Config.Spid + Config.Passwd);//认证码  sha1Encrypt(timestamp+methodName+spid+passwd)
            requestBody.ChargeNbr = Config.ChargeNbr;
            requestBody.Key = Config.Key;
            requestBody.CalleeNbr = mobiles.FirstOrDefault().ToString();
            requestBody.DisplayNbr = Config.DisplayNbr;
            requestBody.TempletId = Config.TempletId;
            requestBody.vReplay = Config.vReplay;
            requestBody.ReplayTTS = Config.ReplayTTS;
            requestBody.ReplayTimes = Config.ReplayTimes;
            requestBody.Value1 = msgContent;
            requestBody.Value2 = Config.Value2;
            requestBody.Value3 = Config.Value3;
            requestBody.Value4 = Config.Value4;
            requestBody.Value5 = Config.Value5;
            requestBody.Value6 = Config.Value6;
            var url = string.Format("{0}?postData={1}", Config.SmsUrl, new XmlSerialize().ModelToXml(requestData));

            try
            {
                var result = GetHtmlData(url);
                string clientIp = string.Empty; //IPHelper.GetClientIp();
                LogUtil.LogInfo("SmsHuawei Return data: " + result + "; client ip: " + clientIp);
                if (!string.IsNullOrWhiteSpace(result))
                {
                    var response = new Response();
                    response = new XmlSerialize().XmlToModel<Response>(result);
                    if (response == null)
                        return clientIp + ": 短信服务商（华为）无法解析返回的Xml文件：" + result;

                    var message = string.Format(clientIp + ": 短信服务商（华为）-手机号码（{0}）发送消息({1}){2}，接口返回消息：{3}。" + Environment.NewLine + "调用路径：{4}",
                    string.Join(",", mobiles), msgContent,
                    response.Head != null && response.Head.Result.Equals("0") ? "成功" : "失败",
                    response.Head != null ? response.Head.ResultDesc : string.Empty,
                    url);
                    LogUtil.LogInfo(message);
                    if (response.Head != null && response.Head.Result.Equals("0"))
                        return null;

                    return message;
                }
                else
                {
                    return clientIp + ": 短信服务商（华为）返回的结果为空，调用API接口地址：" + url;
                }
            }
            catch (Exception ex)
            {
                string message = "短信服务商（华为）调用服务({0})的方法({1})操作{2}。" +
                                 Environment.NewLine + "调用路径：{3}。" +
                                 Environment.NewLine + "错误消息为：{4}";
                message = string.Format(message, ServiceName, MethodBase.GetCurrentMethod().Name, "失败", url, ex.Message);
                LogUtil.LogError(message, ex.StackTrace);
                return message;
            }
        }
    }

    

    
}
